﻿using BigMonitoringWarningApi.Tool;
using BigMonitoringWarningApi.Tool.Extension;
using GYCTOAManagementSystemNewApi.Model.API;
using GYCTOAManagementSystemNewApi.Tool;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BigMonitoringWarningApi.Model.ApiResult
{
    /// <summary>
    ///	日期：2021/8/5 11:45:23 
    ///	描述: ApiPageResult        通用分页信息类    
    /// </summary>s
    public class ApiPageResult
    {

        #region 基类
        private int _pageIndex = 1;
        private int _pageSize = 20;

        /// <summary>
        /// 分页总数
        /// </summary>
        public int PageCount => (int)Math.Ceiling((double)TotalCount / PageSize);

        /// <summary>
        /// 页码(默认:1,最小值:1)
        /// </summary>
        public int PageIndex
        {
            get => _pageIndex;
            set => _pageIndex = Math.Max(value, 1);
        }

        /// <summary>
        /// 页大小(默认:20,最小值:1)
        /// </summary>
        public int PageSize
        {
            get => _pageSize;
            set => _pageSize = Math.Max(value, 1);
        }

        /// <summary>
        /// 状态码
        /// </summary>
        public ApiStatusCodeEnum StatusCode { get; set; }

        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool Status { get; set; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 总行数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 是否存在下一页
        /// </summary>
        public bool HasNext => PageCount > PageIndex;
        #endregion

        /// <summary>
        /// 返回TRUE
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="data">列表数据</param>
        /// <param name="PageIndex">页标</param>
        /// <param name="PageSize">页码大小</param>
        /// <param name="TotalCount">数据总条数</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiPageResult<TData> True<TData>(TData data, int PageIndex, int PageSize, int TotalCount, string message = null)
        {
            return PageResult(data, PageIndex, PageSize, TotalCount, true, message, ApiStatusCodeEnum.Code200);
        }
        /// <summary>
        /// 返回False
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="data">数据</param>
        /// <param name="PageIndex">页标</param>
        /// <param name="PageSize">页码大小</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiPageResult<TData> False<TData>(TData data, int PageIndex, int PageSize, string message = null)
        {
            return PageResult(data, PageIndex, PageSize, 0, false, message, ApiStatusCodeEnum.Code500);
        }
        /// <summary>
        /// 返回False
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="PageIndex">页标</param>
        /// <param name="PageSize">页码大小</param>
        /// <param name="message">返回消息</param>
        /// <returns></returns>
        public static ApiPageResult<TData> False<TData>(int PageIndex, int PageSize, string message = null)
        {
            return PageResult(default(TData), PageIndex, PageSize, 0, false, message, ApiStatusCodeEnum.Code500);
        }

        /// <summary>
        /// 基类返回结果
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <param name="data">数据</param>
        /// <param name="PageIndex">页标</param>
        /// <param name="PageSize">页码大小</param>
        /// <param name="TotalCount">总条数</param>
        /// <param name="message">消息</param>
        /// <param name="Status">返回状态</param>
        /// <param name="StatusCode">状态码Code</param>
        /// <returns></returns>
        public static ApiPageResult<TData> PageResult<TData>(TData data, int PageIndex, int PageSize, int TotalCount, bool Status, string message = null, ApiStatusCodeEnum StatusCode = ApiStatusCodeEnum.Code200)
        {
            return new ApiPageResult<TData>()
            {
                Data = data,
                PageIndex = PageIndex,
                PageSize = PageSize,
                TotalCount = TotalCount,
                StatusCode = StatusCode,
                Status = Status.IsEmpty() ? StatusCode.Equals(ApiStatusCodeEnum.Code200) : Status.ToBool(),
                Message = message.IsEmpty() ? StatusCode.GetEnumDescription() : message
            };
        }
    }
    /// <summary>
    /// 携带Data 的 API返回对象
    /// </summary>
    /// <typeparam name="TData"></typeparam>
    public class ApiPageResult<TData> : ApiPageResult
    {
        private TData _data = default;
        /// <summary>
        /// 构造方法
        /// </summary>
        public ApiPageResult()
            : base()
        {
            _data = default;
        }
        /// <summary>
        /// 操作结果业务数据
        /// </summary>
        public TData Data
        {
            get
            {
                if (typeof(TData).BaseType == typeof(IEnumerator) && _data == null)
                    _data = Activator.CreateInstance<TData>();

                return _data;
            }
            set { _data = value; }
        }
    }
}
